{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torchvision\n",
    "import numpy as np\n",
    "import glob as glob\n",
    "from PIL import Image\n",
    "import torchvision.transforms.functional as TF\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getFrames( path, max_frames = 32, img_h = 360, img_w = 480):\n",
    "    path = path.split(\".\")[0] + \".gi\"\n",
    "    data_root = \"/users/cdwivedi/RL_EXP/IDL/project/tgif-qa/code/dataset/tgif/gifs/\"\n",
    "    files = glob.glob(data_root + path +\"/*\")\n",
    "    frame_idx = torch.arange(len(files), requires_grad=False)\n",
    "    start_idx, end_idx= 0, len(frame_idx)\n",
    "    num_samples = max_frames\n",
    "    index = torch.linspace(start_idx, end_idx, num_samples)\n",
    "    index = torch.clamp(index, 0, frame_idx.shape[0] - 1).long()\n",
    "    frame_idx = torch.index_select(frame_idx, 0, index)\n",
    "    frames = []\n",
    "    for idx in frame_idx:\n",
    "        image = Image.open(files[idx])\n",
    "        image = TF.resize(image, (img_h, img_w))\n",
    "        x = TF.to_tensor(image).unsqueeze(0)\n",
    "        \n",
    "        frames.append(x.permute(0,2,3,1))\n",
    "    frames = torch.cat(frames, dim=0)\n",
    "    return frames"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"/users/cdwivedi/RL_EXP/IDL/project/tgif-qa/code/dataset/tgif/gifs/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = getFrames(\"tumblr_nlhcpzUczA1r7pufno1_500.gi\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_img =[]\n",
    "folders = glob.glob(\"/users/cdwivedi/RL_EXP/IDL/project/tgif-qa/code/dataset/tgif/gifs/*\")\n",
    "for folder in folders:\n",
    "    files = glob.glob(folder+\"/*\")\n",
    "    num_img.append(len(files))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "40.0"
      ]
     },
     "execution_count": 163,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.median(num_img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
